//
//  HomeTabbarViewController.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/14.
//

import UIKit
import VTO2Lib
import SVProgressHUD

class HomeTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.selectedIndex = 0
        self.setupUI()
        
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: hexColor("999999")!], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: hexColor("2A2A2A")!], for: .selected)
        
        if RManager.manager.currentDevice != nil {
            
            self.selectedIndex = 1
            
            VTBLEUtils.sharedInstance().delegate = self
            VTO2Communicate.sharedInstance().delegate = self
            
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd,HH:mm:ss"
            dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
            dateFormatter.formatterBehavior = .default
            let content = dateFormatter.string(from: Date())
            VTO2Communicate.sharedInstance().begin(to: VTParamTypeDate, content: content)
        }
        
    }
    

    func setupUI() {
        let color = UIColor.white
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance.init()
            appearance.backgroundColor = color
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: hexColor("999999")!]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: hexColor("2A2A2A")!]
            self.tabBar.standardAppearance = appearance
            self.tabBar.scrollEdgeAppearance = appearance
        }else {
            self.tabBar.barTintColor = color
        }
    }
    
}

extension HomeTabbarViewController: VTO2CommunicateDelegate, VTBLEUtilsDelegate {
    
    func commonResponse(_ cmdType: VTCmdType, andResult result: VTCommonResult) {
        SVProgressHUD.dismiss()
    }
    
    func didDisconnectedDevice(_ device: VTDevice, andError error: Error) {
        RManager.manager.currentDevice = nil
    }
    
}
